<?php

namespace Admin\Controller;

use Admin\Controller\CommonController;
use Usap\Service\Service;

class UserController extends CommonController
{

    public function index()
    {
        $id = I('request.user_id');
        $user_db = D("Usap/Account/user");
        $user_info = $user_db->where(['user_id' => $id])->find();
//        dprint($user_info);
        $this->assign('user_info', $user_info);
        $this->display("index");
    }

    //初始化密码
    public function initLoginPwdByMobile($user_id)
    {
        $user_service = Service::instance()->find('account', 'user');
        if (IS_POST) {
            $data = I('post.info');
            if ($data['pwd'] == 1) {//手机后6位修改
                $result = $user_service->initLoginPwdByMobile($user_id);
            } elseif ($data['pwd'] == 2) {//手动修改密码
                if ($data['user_password'] !== $data['re_password']) {
                    $this->error('俩次密码输入不一致');
                }
                $result = $user_service->initLoginPwdByNewPwd($user_id, $data['user_password']);
            }
            if ($result['code'] == '0000') {
                $this->success('(:密码初始化成功!');
            } else {
                $this->error("):{$result['msg']}");
            }
        } else {
            $info = $user_service->getInfoById($user_id, array('user_id', 'user_name'));
            $this->assign('info', $info);
            $this->display('init_login_pwd');
        }
    }

    //修改联系方式
    public function editMobile($user_id)
    {
        $user_service = Service::instance()->find('account', 'user');
        $info = $user_service->getInfoById($user_id, array('user_id', 'user_name', 'realname', 'mobile'));
        if (IS_POST) {
            $data = I('post.info');
            $modify_info['mobile'] = $data['mobile'];
            $modify_info['realname'] = $data['realname'];
            $result_str = '';
            $modify_mobile_result = $user_service->modifyUserInfo($user_id, $modify_info);
            if ($modify_mobile_result['code'] != '0000') {
                $this->error($modify_mobile_result['msg']);
            }
            $this->success($result_str . '修改成功');
        } else {
            $this->assign('info', $info);
            $this->display('edit_mobile');
        }
    }

    //导出Excel
    public function outExcel()
    {

        vendor('Excel.PHPExcel');
        vendor('Excel.PHPExcel.Writer.Excel2007');
        vendor('Excel.PHPExcel.Writer.Excel5');
        /*加载类*/
        /*初始化php类*/
        $objPHPExcel = new \PHPExcel();

        /*内容添加*/
        /*1设置excel属性此部分不再页面显示*/
        $objPHPExcel->getProperties()->setCreator("WIFI");//创建人
        /*设置当前的sheet*/
        $objPHPExcel->setActiveSheetIndex(0);
        /*设置sheet的name*/
        $objPHPExcel->getActiveSheet()->setTitle('登录日志Excel');
        /*表格头部*/
        $objPHPExcel->getActiveSheet()->mergeCells('A1:B1');//合并单元格
        $objPHPExcel->getActiveSheet()->setCellValue('A2', '手机号');//设置单元格值
        $objPHPExcel->getActiveSheet()->setCellValue('B2', 'MAC地址');//设置单元格值
        $objPHPExcel->getActiveSheet()->setCellValue('C2', 'IP地址');//设置单元格值
        $objPHPExcel->getActiveSheet()->setCellValue('D2', '智能WiFi ID');//设置单元格值
        $objPHPExcel->getActiveSheet()->setCellValue('E2', '终端');//设置单元格值
        $objPHPExcel->getActiveSheet()->setCellValue('F2', '最近登录时间');//设置单元格值
        $objPHPExcel->getActiveSheet()->setCellValue('G2', '登录次数');//设置单元格值
        $objPHPExcel->getActiveSheet()->setCellValue('A1', '登录日志信息表');//设置单元格的值
        /*添加内容*/
        $mobile = trim($_REQUEST['mobile']);
        $start_date = empty($_REQUEST['start_date']) ? date('Y-m-d', strtotime('-2 days')) : $_REQUEST['start_date'];
        $end_date = empty($_REQUEST['end_date']) ? date('Y-m-d') : $_REQUEST['end_date'];

        $start_time = date('Y-m-d 00:00:00', strtotime($start_date));
        $end_time = date('Y-m-d 23:59:59', strtotime($end_date));

        $condition = "create_time >='$start_time' AND create_time<='$end_time'";
        $order = 'create_time DESC';
        if ($mobile != 'undefined') {
            $condition .= " AND mobile='$mobile'";
            $login_list = D('Usap/Account/UserLoginLog')->field('mobile,max(mac_address) AS mac_address,ip_address,wifi_id,mobile_type,max(create_time) AS create_time,count(id) AS count')->where($condition)->group('ip_address,wifi_id')->order($order)->select();
        } else {
            $login_list = D('Usap/Account/UserLoginLog')->field('*,count(id) AS count')->where($condition)->group('id')->order($order)->select();
        }
        if (empty($login_list)) {

        }
//         $re=M('Message')->select();
//         $count=count($re);
        $i = 3;
        foreach ($login_list as $v) {
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, strval($v['mobile']));//设置单元格值
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, strval($v['mac_address']));//设置单元格值
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, strval($v['ip_address']));//设置单元格值
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, strval($v['wifi_id']));//设置单元格值
            $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, strval($v['mobile_type']));//设置单元格值
            $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, strval($v['create_time']));//设置单元格值
            $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, strval($v['count']));//设置单元格值
            $i++;
        }
        $objPHPExcel->createSheet();
        /*浏览器输出*/
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
        header("Pragma:public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header("Content-Disposition:attachment;filename=登录日志.xls");
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');
        //\ThinkPHP\Library\Vendor\下放入Excel文件夹
    }


    //导出详细日志的Excel
    public function outDetailExcel()
    {

        vendor('Excel.PHPExcel');
        vendor('Excel.PHPExcel.Writer.Excel2007');
        vendor('Excel.PHPExcel.Writer.Excel5');
        /*加载类*/
        /*初始化php类*/
        $objPHPExcel = new \PHPExcel();

        /*内容添加*/
        /*1设置excel属性此部分不再页面显示*/
        $objPHPExcel->getProperties()->setCreator("WIFI");//创建人
        /*设置当前的sheet*/
        $objPHPExcel->setActiveSheetIndex(0);
        /*设置sheet的name*/
        $objPHPExcel->getActiveSheet()->setTitle('详细登录日志Excel');
        /*表格头部*/
        $objPHPExcel->getActiveSheet()->mergeCells('A1:B1:C1');//合并单元格
        $objPHPExcel->getActiveSheet()->setCellValue('A2', '手机号');//设置单元格值
        $objPHPExcel->getActiveSheet()->setCellValue('B2', 'MAC地址');//设置单元格值
        $objPHPExcel->getActiveSheet()->setCellValue('C2', 'IP地址');//设置单元格值
        $objPHPExcel->getActiveSheet()->setCellValue('D2', '智能WiFi ID');//设置单元格值
        $objPHPExcel->getActiveSheet()->setCellValue('E2', '终端');//设置单元格值
        $objPHPExcel->getActiveSheet()->setCellValue('F2', '最近登录时间');//设置单元格值
        $objPHPExcel->getActiveSheet()->setCellValue('A1', '详细登录日志信息表');//设置单元格的值
        /*添加内容*/

        $mobile = trim($_REQUEST['mobile']);
        $ip_address = $_REQUEST['ip_address'];
        $wifi_id = $_REQUEST['wifi_id'];
        $start_date = empty($_REQUEST['start_date']) ? date('Y-m-d', strtotime('-2 days')) : $_REQUEST['start_date'];
        $end_date = empty($_REQUEST['end_date']) ? date('Y-m-d') : $_REQUEST['end_date'];

        $start_time = date('Y-m-d 00:00:00', strtotime($start_date));
        $end_time = date('Y-m-d 23:59:59', strtotime($end_date));

        $order = 'create_time DESC';
        $condition = "mobile='$mobile' AND ip_address='$ip_address' AND wifi_id='$wifi_id' AND create_time >='$start_time' AND create_time<='$end_time'";
        $login_list = D('Usap/Account/UserLoginLog')->where($condition)->order($order)->select();

        if (empty($login_list)) {
// 			$this->error('不能导出空文件！');
// 			exit();
        }
        $i = 3;
        foreach ($login_list as $v) {
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, strval($v['mobile']));//设置单元格值
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, strval($v['mac_address']));//设置单元格值
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, strval($v['ip_address']));//设置单元格值
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, strval($v['wifi_id']));//设置单元格值
            $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, strval($v['mobile_type']));//设置单元格值
            $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, strval($v['create_time']));//设置单元格值
            $i++;
        }
        $objPHPExcel->createSheet();
        /*浏览器输出*/
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
        header("Pragma:public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header("Content-Disposition:attachment;filename=详细登录日志.xls");
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');
        //\ThinkPHP\Library\Vendor\下放入Excel文件夹
    }

    /**
     * 获取随机ID
     */
    public function genRandGwId()
    {
        $rand_id = substr(uniqid(), -5) . substr(uniqid(), -5) . substr(uniqid(), -5);
        return $this->ajaxReturn(array('code' => '0000', 'rand_id' => $rand_id));
    }
} 
